<?php
defined('IN_MAIN') or exit('Invalid Access!');

function retrieve_gen_downline($in_upline_id, $which_network, $aj_query_name, $retrieve_level = 1){
	global $vars;
	$db = $vars['db'];
	
	$this_month['start'] = date('Y-m-d H:i:s', mktime(0,0,0,ndate('n'),1,ndate('Y')));
	$this_month['end'] = date('Y-m-d H:i:s', mktime(23,59,59,ndate('n')+1,0,ndate('Y')));
	$last_month['start'] = date('Y-m-d H:i:s', mktime(0,0,0,ndate('n')-1,1,ndate('Y')));
	$last_month['end'] = date('Y-m-d H:i:s', mktime(23,59,59,ndate('n'),0,ndate('Y')));
	$l_month = date('n', strtotime($last_month['start']));
	$l_year = date('Y', strtotime($last_month['start']));

	$cur_level = 0;
	$uplines[$cur_level][0] = $in_upline_id;
	do{
		$nli = 0;
		$next_level = $cur_level + 1;
		for($i=0,$t=count($uplines[$cur_level]);$i<$t;$i++){
			$upline_id = $uplines[$cur_level][$i];
			$t2=@mysql_num_rows($r2=mysql_query("select id  from $db->users where $which_network='$upline_id'".($in_upline_id==$vars['genealogy_head_id']? " and id not in ($vars[genealogy_hide_uid])" : "")." and status!='terminated' order by cdate"));
			for($j=0;$j<$t2;$j++){
				$downline = mysql_fetch_assoc($r2);
				$downline = get_user_detail_by_id($downline['id']);
				$have_downline = @mysql_result(mysql_query("select count(*) from $db->users where $which_network='$downline[id]'".($in_upline_id==$vars['genealogy_head_id']? " and id not in ($vars[genealogy_hide_uid])" : "")." and status!='terminated' order by cdate"), 0) or 0;
				if($downline["acc_type"]=="c"){
					$display_pic=$downline['acc_type'];
				}elseif($downline["gender"]=="m"){
					$display_pic=$downline['gender'];
				}else{
					$display_pic='f';
				}
				//ps
				//this month
				$this_month_sales_maintain = calc_personal_sales($downline['id'], $this_month['start'], $this_month['end']);
				
				$display_pic = JS_URL."/jqtree/images/node{$display_pic}.gif";
				$strlen = strlen($downline['name']);
				$downline_width=$vars['gen_tweak']['base_info_width'] + ($strlen * ($vars['gen_tweak']['char_width']) + 10);
				$display_value="<span class='gen_img'><img src='$display_pic' /></span> <span class='gen_id'>".$downline["id"]."</span>".
				"<span class='gen_jdate'>Joined ".date($vars['date_format_short'], strtotime($downline["cdate"]))."</span>".
				" <span class='gen_pspgs'>".__('Sales').": ".number_format($this_month_sales_maintain, 2)."</span>";
				$downlines[$upline_id][$j]['id'] = $downline['id'];
				$downlines[$upline_id][$j]['width'] = $downline_width + 100;
				$downlines[$upline_id][$j]['username'] = $downline['username'];
				$downlines[$upline_id][$j]['node'] = "<div class='tree-node'>$display_value</div><div class='clear'></div>";
				$downlines[$upline_id][$j]['have_downline'] = ($next_level >= $retrieve_level && $have_downline)? true : false;
				$uplines[$next_level][$nli++] = $downline['id'];
			}
		}
		$cur_level++;
	}while($cur_level < $retrieve_level && count($uplines[$cur_level]));
	unset ($uplines);
	if($downlines){
		$upline_id = $in_upline_id;
		$uplines[0] = $upline_id;
		do{
			$ui = 0;
			unset($next_uplines);
			for($i=0,$t=count($uplines);$i<$t;$i++){
				$upline_id = $uplines[$i];
				unset($replace_later);
				for($j=0,$t2=count($downlines[$upline_id]);$j<$t2;$j++){
					$downline = $downlines[$upline_id][$j];
					$this_node = "
					<li id='$downline[id]' style='width:{$downline['width']}px;'>$downline[node]".($downline['have_downline']? "<ul class='ajax'><li>{url:$vars[this_file]?$aj_query_name=1&id=$downline[id]&aj=1}</li></ul>" : "")."{{$downline[id]}}</li>";
					if(strstr($downline_node, "{{$upline_id}}")){
						$replace_later .= $this_node;
					}else{
						$downline_node .= $this_node;
					}
					$next_uplines[$ui++] = $downline['id'];
				}
				if($replace_later){
					$downline_node = str_replace("{{$upline_id}}", "\n<ul>$replace_later</ul>{{$upline_id}}", $downline_node);
				}
			}
			$uplines = $next_uplines;
		}while($next_uplines);
		$downline_node = preg_replace('/\{[0-9]+\}/', '', $downline_node);
	}
	
	return $downline_node;
}
?>